home *** CD-ROM | disk | FTP | other *** search
- Path: erich.triumf.ca!bennett
- From: bennett@erich.triumf.ca (P.Bennett)
- Newsgroups: comp.lang.c
- Subject: Re: Help for a float problem
- Date: 9 Feb 1996 22:28 PST
- Organization: TRIUMF: Tri-University Meson Facility
- Distribution: world
- Message-ID: <9FEB199622285820@erich.triumf.ca>
- References: <AMCHAVAN.96Jan25112007@ac2.hq.eso.org> <4f6tt6$rt1@news.cencom.net> <311A83D1.57A3@feith.com> <4fe4m4$f1c@news.ee.net> <4fgbss$nqm@ixnews6.ix.netcom.com>
- NNTP-Posting-Host: ftp.triumf.ca
- News-Software: VAX/VMS VNEWS 1.50
-
- In article <4fgbss$nqm@ixnews6.ix.netcom.com>, wzjn@ix.netcom.com (KPN ) writes...
- >Good Afternoon,
- >
- >Using æHow To Program C (second edition)Æ by Deitel & Deitel, IÆm
- >trying to do on page 92, example 3.17. IÆm not sure, but believe that
- >my problem is where to use a æfloatÆ.
- >
- >I want to ask the user to enter gallons used, which of course, could be
- >12.8. However, with the same line, I want the user to enter æ-1Æ if
- >they choose to end the program.
- >
- >The way it is written so far, the user can successfully exit the
- >program with a -1. The user can also successfully enter whole numbers
- >and get an answer. My problem arises when I enter (again) 12.8 ... the
- >program goes in to an infinate loop! eh?
- >
- >Conversely, if I define gallons as a float, the program does not
- >recognize the æ-1Æ as an end of program. eh?
- >
-
- If you expect the user to enter a decimal value like 12.8 for gallons (or
- miles), you must declare gallons as float.
-
- However, when the user enters -1 to quit, you will be caught by the fact that
- floating point numbers are never exact (Although I would expect -1 to be), so
- it is unwise to compare two floats for equality.
-
- You could compare their difference to some small number, or, for this problem,
- simply consider any negative number as the signal to quit.
-
-
- Peter Bennett VE7CEI | Vessels shall be deemed to be in sight
- Internet: bennett@triumf.ca | of one another only when one can be
- Packet: ve7cei@ve7kit.#vanc.bc.ca | observed visually from the other
- TRIUMF, Vancouver, B.C., Canada | ColRegs 3(k)
- GPS and NMEA info and programs: ftp://sundae.triumf.ca/pub/peter/index.html
-
-
-
-
-
-
-
-
-
-